package com.wthink.newMain.settings.config.inner.calendar

import android.app.Activity
import android.app.DatePickerDialog
import android.app.DatePickerDialog.OnDateSetListener
import android.app.TimePickerDialog
import android.content.ComponentName
import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.AdapterView
import android.widget.TextView
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import com.wthink.hzgj.R
import com.wthink.hzgj.databinding.FragmentCalendarSettingsBinding
import com.wthink.hzgj.util.toast
import kotlinx.android.synthetic.main.fragment_calendar_settings.*
import java.util.*


class CalendarSettingsFragment : Fragment() {

    private val viewModel by viewModels<CalendarSettingsViewmodel>()

    private lateinit var binding: FragmentCalendarSettingsBinding

    var calendar: Calendar? = null

    var host = ""
    var gps = "0"
    var ntp = "0"
    var server = "0"

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = DataBindingUtil.inflate(
            inflater,
            R.layout.fragment_calendar_settings
            ,
            container,
            false
        )
        binding.viewModel = viewModel
        binding.lifecycleOwner = this
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        calendar = Calendar.getInstance(Locale.CHINA)
        viewModel.getData {
            activity?.runOnUiThread {
                ivWeixing.isSelected = it.gps == "1"
                ivNtp.isSelected = it.ntp == "1"
                ivServer.isSelected = it.server == "1"
                gps = it.gps
                ntp = it.ntp
                server = it.server
                it.host_list.forEachIndexed { index, s ->
                    if (s == it.host) {
                        spinnerNTP.setSelection(index)
                        return@forEachIndexed
                    }
                }
            }
        }

        viewModel.getTime {
            activity?.runOnUiThread {
                val date = Date(it.toLong())
                calendar?.time = date
            }
        }

        ivNtp.setOnClickListener {
            when {
                ivNtp.isSelected -> {
                    ntp = "0"
                    ivNtp.isSelected = false
                }
                else -> {
                    ntp = "1"
                    ivNtp.isSelected = true
                }
            }

        }
        ivWeixing.setOnClickListener {
            when {
                ivWeixing.isSelected -> {
                    gps = "0"
                    ivWeixing.isSelected = false
                }
                else -> {
                    gps = "1"
                    ivWeixing.isSelected = true
                }
            }
        }
        ivServer.setOnClickListener {
            when {
                ivServer.isSelected -> {
                    server = "0"
                    ivServer.isSelected = false
                }
                else -> {
                    server = "1"
                    ivServer.isSelected = true
                }
            }

        }

        binding.spinnerNTP.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onNothingSelected(parent: AdapterView<*>?) {

            }

            override fun onItemSelected(
                parent: AdapterView<*>?,
                view: View?,
                position: Int,
                id: Long
            ) {
                host = viewModel.autoTimingData.value!!.host_list[position]
            }
        }

        binding.btnConfig.setOnClickListener {
            if (ntp.isEmpty() || gps.isEmpty() || server.isEmpty() || host.isEmpty()) {
                toast(activity!!, "请配置完整信息")
            }
            viewModel.config(gps, ntp, host, server)
        }

        binding.tvDate.setOnClickListener {
            selectDate(activity!!, 4, tv_date, calendar!!)
        }

        binding.tvTime.setOnClickListener {
            selectTime(activity!!, 4, tv_time, calendar!!)
        }

        binding.btnSet.setOnClickListener {
            val date = tv_date.text.toString()
            val time = tv_time.text.toString()
            viewModel.setTime("${date}_$time")
        }
    }

    fun selectDate(
        activity: Activity?,
        themeResId: Int,
        textView: TextView,
        calendar: Calendar
    ) {
        DatePickerDialog(
            activity!!, themeResId,
            OnDateSetListener { view, year, monthOfYear, dayOfMonth ->
                if (monthOfYear >= 10) {
                    if (dayOfMonth >= 10) {
                        textView.text = "$year-${monthOfYear + 1}-$dayOfMonth"
                    } else {
                        textView.text = "$year-${monthOfYear + 1}-0$dayOfMonth"
                    }
                } else {
                    if (dayOfMonth >= 10) {
                        textView.text = "${year}-0${monthOfYear + 1}-$dayOfMonth"
                    } else {
                        textView.text = "${year}-0${monthOfYear + 1}-0$dayOfMonth"
                    }
                }
            }
            , calendar[Calendar.YEAR]
            , calendar[Calendar.MONTH]
            , calendar[Calendar.DAY_OF_MONTH]).show()
    }

    fun selectTime(
        activity: Activity?,
        themeResId: Int,
        textView: TextView,
        calendar: Calendar
    ) {
        TimePickerDialog(activity, themeResId,
            TimePickerDialog.OnTimeSetListener { view, hourOfDay, minute ->
                if (hourOfDay >= 10) {
                    if (minute >= 10) {
                        textView.text = "$hourOfDay:$minute"
                    } else {
                        textView.text = "$hourOfDay:0$minute"
                    }
                } else {
                    if (minute >= 10) {
                        textView.text = "0$hourOfDay:$minute"
                    } else {
                        textView.text = "0$hourOfDay:0$minute"
                    }
                }
            }
            , calendar.get(Calendar.HOUR_OF_DAY)
            , calendar.get(Calendar.MINUTE)
            , true).show()
    }
}